/**
 * 规格选择状态保持测试
 * 验证选择第二个规格后，第一个规格选择项不会发生改变
 */

// 模拟规格数据
const mockSpecs = [
    {
        name: "颜色",
        vs: [
            { value: "红色", selected: false, stockStatus: 'normal', disabled: false },
            { value: "蓝色", selected: false, stockStatus: 'normal', disabled: false }
        ]
    },
    {
        name: "尺码",
        vs: [
            { value: "大码", selected: false, stockStatus: 'normal', disabled: false },
            { value: "小码", selected: false, stockStatus: 'normal', disabled: false }
        ]
    }
];

// 模拟选择规格的过程
function simulateSpecSelection(specs, selectedSpecs, newSpecName, newSpecValue) {
    console.log(`\n=== 选择规格: ${newSpecName} = ${newSpecValue} ===`);
    console.log('选择前的规格状态:', JSON.stringify(specs, null, 2));
    
    // 更新选择规格数组
    const updatedSelectSpecs = [...selectedSpecs];
    const index = updatedSelectSpecs.findIndex(item => item.name === newSpecName);
    if (index === -1) {
        updatedSelectSpecs.push({ name: newSpecName, v: newSpecValue });
    } else {
        updatedSelectSpecs[index].v = newSpecValue;
    }
    
    console.log('更新后的选择规格数组:', updatedSelectSpecs);
    
    // 更新规格的选中状态
    const updatedSpecs = specs.map(specItem => {
        if (specItem.name === newSpecName) {
            return {
                ...specItem,
                vs: specItem.vs.map((v, idx) => ({
                    ...v,
                    selected: v.value === newSpecValue
                }))
            };
        }
        return specItem;
    });
    
    console.log('更新后的规格状态:', JSON.stringify(updatedSpecs, null, 2));
    
    // 检查第一个规格的状态是否保持
    const firstSpec = updatedSpecs[0];
    const firstSpecSelected = firstSpec.vs.find(v => v.selected);
    
    console.log(`第一个规格 "${firstSpec.name}" 的选中状态:`, firstSpecSelected ? firstSpecSelected.value : '无选中');
    
    return {
        specs: updatedSpecs,
        selectSpecs: updatedSelectSpecs
    };
}

// 测试执行
console.log('=== 规格选择状态保持测试 ===');

console.log('1. 初始规格数据:');
console.log(JSON.stringify(mockSpecs, null, 2));

let currentSpecs = [...mockSpecs];
let currentSelectSpecs = [];

console.log('\n2. 第一次选择: 颜色 = 红色');
const result1 = simulateSpecSelection(currentSpecs, currentSelectSpecs, "颜色", "红色");
currentSpecs = result1.specs;
currentSelectSpecs = result1.selectSpecs;

console.log('\n3. 第二次选择: 尺码 = 大码');
const result2 = simulateSpecSelection(currentSpecs, currentSelectSpecs, "尺码", "大码");
currentSpecs = result2.specs;
currentSelectSpecs = result2.selectSpecs;

console.log('\n4. 验证第一个规格状态:');
const firstSpec = currentSpecs[0];
const firstSpecSelected = firstSpec.vs.find(v => v.selected);
console.log(`第一个规格 "${firstSpec.name}" 的选中状态:`, firstSpecSelected ? firstSpecSelected.value : '无选中');

if (firstSpecSelected && firstSpecSelected.value === '红色') {
    console.log('✅ 测试通过：第一个规格选择状态正确保持');
} else {
    console.log('❌ 测试失败：第一个规格选择状态发生改变');
}

console.log('\n=== 测试完成 ===');
